<?php

	class GenerateModelFilesCommand implements ICommand
	{
		public function execute($event)
		{
			$module 		= $event->data['module'];
			$name 			= $event->data['name'];
			$tablename 		= $event->data['tablename'];
			$columns 		= $event->data['columns'];
			$generate_view 	= $event->data['viewsyn'];
			$generate_cmd 	= $event->data['cmdyn'];
			
			$key 			= '';
			$contentTable 	= '';
			$fields			= array();
									
			$modulefilemodel 	= EbModel::getModel('Core','ModuleFile');			
			
			foreach ($columns AS $column)
			{
				$column = (object) $column;
				
				if ($column->key == 1)
				{
					$key = $column->name;
					$column->autoincrement = 'true';
				}
				else
				{
					$fields []= $column->name;
				}				
				
				$not_null = !$column->not_null;
				
				$contentTable .= "\t\t\t\tnew EbTableColumn('$column->name','$column->type',".EbToolkit::boolean2string($not_null).",".$column->key.",".EbToolkit::boolean2string($column->autoincrement)."),\n";
				
				$contentVO .= "\t\tvar $".$column->name.";\n";
			}
			
			$contentTable = substr($contentTable,0,strlen($contentTable) - 2);	

			$contentModel = "
		public function __construct()
		{
			parent::init('$tablename','".$name."VO','$key');
		}
		
		public function getColumns()
		{
			return array(
$contentTable
			);
		}
		";
			
			$modulefilemodel->insert((object) array('module' => $module,'filename' => $name,'type' => 'VO','content' => $contentVO));
			$modulefilemodel->insert((object) array('module' => $module,'filename' => $name,'type' => 'Model','extends' => 'extends EbTableSQLModel','content' => $contentModel));
			
			if ($generate_view == 'true')
			{
				return new Event('Core/GenerateViewFiles',array('module' => $module,'name' => $name, 'fields' => $fields));
			}
			
			if ($generate_cmd == 'true')
			{
				
			}
			
			return $event->data;
		}
	}